// $Id: partitionnodeiterative.cpp 65 2004-10-27 21:44:50Z hansemil $
#include "partitionnodeiterative.h"
#include "partitionizer.h"
#include "loghandler.h"

/**
 * @todo Write documentation
 */
PartitionNodeIterative::PartitionNodeIterative()
{
}

/// @todo Note that Iterrative does deep-style(?) inserts.

/**
 * This method will do a request for a file. It will continue to do the iterative steps in the partition iterative serach scheme untill the file is found or we run out of partitions to try.
 */
bool PartitionNodeIterative::requestFile(File *file, int hopsToLive)
{
	m_searchPartition = 0;
	bool res;
	NodeRequestFileParameters params(m_nextSearchGuid, hopsToLive, file, false);
	do{
		// Reset NodeRequestFileParameters parameters
		params.m_guid = m_nextSearchGuid++;
		params.m_hopsToLive = hopsToLive;
		params.m_hopCount = 0;

		// Search for the file
		res = requestFile(((Node *)this), &params);
		m_searchPartition++;
	}while(!res && m_searchPartition < Partitionizer::getInstance()->getPartitionCount());

	// Log the result
	LogHandler::getInstance()->node_requestFile(this, &params, res);
	return res;
}

/**
 * @todo Write documentation
 */
void PartitionNodeIterative::discriminateRequestOrderList(std::list<KeyDistanceHelper> &list)
{
	// Take the list and discriminate according to the current partition search depth and order by partition
	discriminateRequestOrderListPartition(list, m_searchPartition);
}
